const path = require('path')
const { defineConfig } = require('@vue/cli-service')
const { VantResolver } = require('@vant/auto-import-resolver');
const AutoImport = require('unplugin-auto-import/webpack');
const Components = require('unplugin-vue-components/webpack');

const pathSrc = path.resolve(__dirname, 'src')

module.exports = defineConfig({
  pages: {
    index: {
      entry: 'src/main.js', // 入口文件
      title: '数字商铺'
    }
  },
  transpileDependencies: true,
  devServer: {
    proxy: {
      // 新增: 代理第三方(或外部) 数据服务，解决 CORS (szsp 平台 BIM 接口)
      // 注意：这个规则必须在 /api 之前，因为 /szsp/api 会匹配到更通用的 /api 规则
      "/szsp": {
        target: "https://szjfadmin.zhulin.xin",
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          "^/szsp": "" // 去掉前缀，直接转发到目标主机
        }
      },
      '/api': {
        target: process.env.VUE_APP_BASE_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': pathSrc,
      },
      fallback: {
        "canvas": false,
        "fs": false,
        "http": false,
        "https": false,
        "url": false,
        "zlib": false
      }
    },
    plugins: [
      AutoImport({
        // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
        imports: ['vue'],
        // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
        resolvers: [
          VantResolver()
        ],

        dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
      }),
      Components({
        resolvers: [
          VantResolver(),
        ],

        dts: path.resolve(pathSrc, 'components.d.ts'),
      })
    ]
  }
})
